'use strict';
const nodemailer = require('nodemailer'); // 引入模块

const user = {
    user: '1623719577@qq.com', // 用户
    pass: 'adpjyzdlsuofbcdf' // 密码
};

let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com', // 主机名
    port: 465, // 端口号
    secure: true, // true for 465, false for other ports
    auth: user
});

function sendMail(email) {
    let code = getCode();

    transporter.sendMail({
        from: '"森屿暖鲸" <1623719577@qq.com>', // 发送地址
        to: email, // 接收地址
        subject: '验证码', // 主题
        html: `<b>您的验证码是:${code}</b>` // html信息
    });

    return code;
}

function random(min, max) {
    return Math.ceil(Math.random() * (max - min + 1) + min - 1);
}

function getCode() {
    var code = '';
    var type;
    for (var i = 0; i < 6; i++) {
        type = random(1, 3);
        switch (type) {
            case 1:
                code += String.fromCharCode(random(48, 57));
                break;
            case 2:
                code += String.fromCharCode(random(65, 90));
                break;
            case 3:
                code += String.fromCharCode(random(97, 122));
                break;
        }
    }
    return code;
}

module.exports = sendMail;
